// common/nyz_area_picker/nyz_area_picker.js
var areaTool = require('../../utils/area.js')
var index = [0, 0, 0]
var provinces = areaTool.getProvinces()
var citys = areaTool.getCitys(index[0])
var areas = areaTool.getAreas(index[0], index[1])

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      //控制area_select显示隐藏
      type: Boolean,
      value: false
    },
    maskShow: {
      //是否显示蒙层
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    provinces: provinces,
    citys: areaTool.getCitys(index[0]),
    areas: areaTool.getAreas(index[0], index[1]),
    value: [0, 0, 0],
    province: '北京市',
    city: '市辖区',
    area: '东城区',
    change: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindtouch() {
      //解决滚动过快
      this.data.change = false
      // loading
      wx.showLoading({
        title: '加载中'
      })
    },
    handleNYZAreaChange: function(e) {
      var that = this
      var value = e.detail.value
      /**
       * 滚动的是省
       * 省改变 市、区都不变
       */
      let selectCitys = []
      let selectAreas = []
      if (index[0] != value[0]) {
        index = [value[0], 0, 0]
        selectCitys = areaTool.getCitys(index[0])
        selectAreas = areaTool.getAreas(index[0], 0)
        if (selectCitys.length > 0 || selectAreas.length > 0) {
          that.setData({
            citys: selectCitys,
            areas: selectAreas,
            value: [index[0], 0, 0],
            province: provinces[index[0]],
            city: selectCitys[0],
            area: selectAreas[0],
            change: true
          })
        }
      } else if (index[1] != value[1]) {
        /**
         * 市改变了 省不变 区变
         */
        index = [value[0], value[1], 0]
        selectCitys = areaTool.getCitys(index[0])
        selectAreas = areaTool.getAreas(index[0], value[1])
        if (selectCitys.length > 0 || selectAreas.length > 0) {
          that.setData({
            citys: selectCitys,
            areas: selectAreas,
            value: [index[0], index[1], 0],
            province: provinces[index[0]],
            city: selectCitys[index[1]],
            area: selectAreas[0],
            change: true
          })
        }
      } else if (index[2] != value[2]) {
        /**
         * 区改变了
         */
        index = [value[0], value[1], value[2]]
        selectCitys = areaTool.getCitys(index[0])
        selectAreas = areaTool.getAreas(index[0], value[1])
        if (selectCitys.length > 0 || selectAreas.length > 0) {
          that.setData({
            citys: selectCitys,
            areas: selectAreas,
            value: [index[0], index[1], index[2]],
            province: provinces[index[0]],
            city: selectCitys[index[1]],
            area: selectAreas[index[2]],
            change: true
          })
        }
      }
      // loading
      wx.hideLoading()
    },
    /**
     * 确定按钮的点击事件
     */
    handleNYZAreaSelect: function(e) {
      const self = this
      var myEventDetail = e // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      if (this.data.change) {
        self.triggerEvent('sureSelectArea', myEventDetail, myEventOption)
        self.data.change = false
      }
      // var myEventOption = {}; // 触发事件的选项
      // this.triggerEvent('sureSelectArea', myEventDetail, myEventOption)
    },
    /**
     * 取消按钮的点击事件
     */
    handleNYZAreaCancle: function(e) {
      var that = this
      that.setData({
        show: false
      })
    }
  }
})
